package com.blogs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blogs.component.BaseActivity;
import com.blogs.service.SendCommentService;
import com.blogs.tools.DBSharedPreferences;
import com.cnblogs.R;
import com.cnblogs.R.color;

public class NewComment extends BaseActivity implements OnClickListener {
	private RelativeLayout title_top_bg;
	private Button title_include_back;
	private TextView title_include_title;
	private Button new_comment_send_btn;
	private EditText new_commnet_et;
	private Toast toast;
	private int type;
	private String comment_id;
	private String parentCommentID;
	private String Author;
	private String blogapp;
	private String root;
	
	private DBSharedPreferences sp;
	private RelativeLayout new_com_bg;
	private RelativeLayout new_comment_buttom_ll;
	private LinearLayout new_com_cb_ll;
	private CheckBox radioButton_from;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_comment);
		sp= new DBSharedPreferences(this);
		if(sp.GetSessionID().equals(""))
		{
			this.showToast("请先登录");
			Intent intent=new Intent();
			intent.setClass(NewComment.this, UserLoginActivity.class);
			startActivity(intent);
			finish();
		}
		ViewGroup headview = (ViewGroup) findViewById(R.id.main_title_include);
    	title_top_bg=(RelativeLayout)headview;
		title_include_title = (TextView) headview.findViewById(R.id.title_include_title);
		title_include_back = (Button) headview.findViewById(R.id.title_include_back);
		radioButton_from=(CheckBox)findViewById(R.id.radioButton_from);
		title_include_title.setText("发评论");
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		new_com_bg=(RelativeLayout)findViewById(R.id.new_com_bg);
		new_comment_buttom_ll=(RelativeLayout)findViewById(R.id.new_comment_buttom_ll);
		new_comment_send_btn = (Button) findViewById(R.id.new_comment_send_btn);
		new_com_cb_ll=(LinearLayout)findViewById(R.id.new_com_cb_ll);
		new_comment_send_btn.setOnClickListener(this);
		new_com_cb_ll.setOnClickListener(this);
		new_commnet_et=(EditText)findViewById(R.id.new_commnet_et);
		type=getIntent().getIntExtra("type", 0);
		comment_id=getIntent().getStringExtra("comment_id");
		parentCommentID=getIntent().getStringExtra("parentCommentID");
		root=getIntent().getStringExtra("root");
		Author=getIntent().getStringExtra("Author");
		blogapp=getIntent().getStringExtra("blogapp");
		if(!parentCommentID.equals("0"))
		{
			new_commnet_et.setText("@"+Author+"\n");
			if(root!=null)
			{
				new_commnet_et.append("[quote]"+root+"[/quote]\n");
			}
			new_commnet_et.setSelection(new_commnet_et.length());
		}
		this.InitSkin();
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			new_com_bg.getBackground().setLevel(2);
			new_comment_buttom_ll.getBackground().setLevel(2);
			new_commnet_et.setTextColor(getResources().getColor(color.title_text_night));
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			new_com_bg.getBackground().setLevel(1);
			new_comment_buttom_ll.getBackground().setLevel(1);
			new_commnet_et.setTextColor(getResources().getColor(color.title_text_light));
		}
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==new_comment_send_btn)
		{
			if(new_commnet_et.getEditableText().toString().trim().length()==0)
			{
				showToast("亲,你好像没有写东西哦!");
				return;
			}
			Intent intent=new Intent(this,SendCommentService.class);
			intent.putExtra("sid", sp.GetSessionID());
			intent.putExtra("type", type);
			intent.putExtra("niceNmae", sp.GetNickName());
			intent.putExtra("comment_id", comment_id);
			intent.putExtra("parentCommentID", parentCommentID);
			intent.putExtra("is_add_from", radioButton_from.isChecked()?1:0);//是否添加来源
			intent.putExtra("commendBody", new_commnet_et.getEditableText().toString());
			intent.putExtra("blogapp", blogapp);
	        startService(intent);
			this.showToast("评论已进入发送队列");
			this.finish();
		}
		else if(v==new_com_cb_ll)
		{
			radioButton_from.setChecked(!radioButton_from.isChecked());
		}
	}
	private void showToast(String text)
	{
		if (toast == null) {
			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
}
